#pragma once
#ifndef _GLENV_H
#define _GLENV_H

#include <Windows.h>
#include <gl/glut.h>

#include "Ptr.hpp"

class GlControl;

void glenv_create_window (UINT mode, const char* title, bool isFullScreen,
  int width = 0, int height = 0, int posX = 0, int posY = 0);

void glenv_run (Ptr< GlControl > control);


class GlControl
{
private:
  int _timerDuration;
  int _timerInitValue;

public:
  GlControl () : _timerDuration (0), _timerInitValue (0) {}

  // enable opengl feature and set default background color here
  virtual void init_render () = 0;

  virtual void draw () = 0;

  virtual void handle_keypress (unsigned char key, int x, int y) {}
  virtual void handle_special_keypress (int key, int x, int y) {}
  virtual void handle_mouse (int button, int state, int x, int y) {}
  virtual void handle_motion (int x, int y) {}
  virtual void handle_passive_motion (int x, int y) {}

  virtual int handle_timer (int val) { return 0; }

  virtual void handle_reshape (int width, int height) {
    glViewport (0, 0, width, height);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (45.0, (double)width / (double)height, 1.0, 200.0);
    glutPostRedisplay ();
  }
  
  virtual int getTimerDuration () const { return _timerDuration; }
  virtual void setTimerDuration (int millisecond) { _timerDuration = millisecond; }
  
  // timer init val is the init value that handle timer receive
  virtual int getTimerInitValue () const { return _timerInitValue; }
  // timer init val is the init value that handle timer receive
  virtual void setTimerInitValue (int val) { _timerInitValue = val; }
};

#endif